from sqlalchemy import Column, DateTime, Float, Integer, String, func
from sqlalchemy.orm import relationship

from app.bot.services.date_time import get_date_time
from app.database.models.base import Base
from app.database.models.m2ms import server_categories, sites_categories


class Site(Base):
    __tablename__ = "site"

    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    categories = relationship(
        "Category", secondary=sites_categories, back_populates="sites"
    )
    orders = relationship("Order", back_populates="site")

    def __str__(self):
        return self.name

    def __repr__(self):
        return f"<Site №{self.id} {self.name}>"


class Category(Base):
    __tablename__ = "category"

    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False)
    name_eng = Column(String(50), nullable=False)
    sites = relationship(
        "Site", secondary=sites_categories, back_populates="categories"
    )
    servers = relationship(
        "Server", secondary=server_categories, back_populates="categories"
    )
    orders = relationship("Order", back_populates="category")
    price_4h = Column(Float, nullable=False)
    price_1d = Column(Float, nullable=False)
    price_1w = Column(Float, nullable=False)
    price_2w = Column(Float, nullable=False)
    price_1m = Column(Float, nullable=False)
    created_at = Column(DateTime, server_default=func.now())

    def __str__(self):
        return self.name

    def __repr__(self):
        return f"<Category №{self.id} {self.name}>"
